const payment = {
	data() {
		return {
			provider: '',
			createOrderDetails: {},
		}
	},

	methods: {
		// 支付 paymentType 区分支付类型1普通商品2积分商品3充值 isReturn支付失败时是否直接return蓝牙商品isBluetooth
		payment(paymentType = 1, isReturn = false, isBluetooth = false) {
			const that = this
			if (this.payMethod == 1) {
				if (isBluetooth) {
					// console.log(that)
					// that.bluetoothCallback()
				} else {
					uni.redirectTo({
						url: `/pages/success/index?createOrderDetails=${encodeURIComponent(JSON.stringify(this.createOrderDetails))}&paymentType=${paymentType}`
					})
				}
			} else {
				uni.getProvider({
					service: 'payment',
					success: (result) => {
						result.provider.map((value) => {
							that.provider = value
							console.log(value, 'valuexx')
							let payment = {}
							if (that.provider == 'wxpay') {
								payment = that.createOrderDetails.weixinPayParam
							} else {
								// alipay
								payment = that.createOrderDetails.aliPayParam
							}
							that.$store.commit('user/UPDATE_WXPAY', true);
							const {
								timeStamp,
								nonceStr,
								package_,
								signType,
								paySign
							} = payment
							uni.requestPayment({
								provider: that.provider,
								timeStamp: timeStamp,
								nonceStr: nonceStr,
								package: package_,
								signType: signType,
								paySign: paySign,
								success(res) {
									if (isBluetooth) {
										uni.navigateBack()
									} else {
										uni.redirectTo({
											url: `/pages/success/index?createOrderDetails=${encodeURIComponent(JSON.stringify(that.createOrderDetails))}&paymentType=${paymentType}`
										})
									}
									that.$store.commit('user/UPDATE_WXPAY', false);
								},
								fail(res) {
									if (isReturn) {
										return
									}
									that.$store.commit('user/UPDATE_WXPAY', false);
									if (isBluetooth) {
										console.log('关闭定时器')
										clearInterval(that.timer)
									} else {
										uni.redirectTo({
											url: `/pages/order/index?orderStatus=0`
										})
									}
								}


							})
						})
					},
					fail: (error) => {
						console.log('获取登录通道失败', error);
					}
				});
			}
		}
	}
}
export default payment
